package com.demo.event.service;

import com.demo.event.dto.UserDto;
import com.demo.event.dto.UserLoginEvent;
import com.demo.event.dto.UserLoginEvent01;
import com.demo.event.dto.UserLoginEvent02;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 *
 * @author
 * @since
 */
@Component
@Slf4j
public class UserLoginServiceImpl implements UserLoginService, ApplicationContextAware {

  private ApplicationContext applicationContext;

  @Override
  public void loginSucc(UserDto userDto) {
    Assert.notNull(applicationContext, "ApplicationContext was not init.");
    Assert.notNull(userDto, "传入 UserDto不能为空");
    applicationContext.publishEvent(new UserLoginEvent(this, userDto));
    log.info("已发布 loginSucc 事件UserLoginEvent. {}", userDto);
  }

  @Override
  public void loginSucc01(UserDto userDto) {
    Assert.notNull(applicationContext, "ApplicationContext was not init.");
    Assert.notNull(userDto, "传入 UserDto不能为空");
    applicationContext.publishEvent(new UserLoginEvent01(this, userDto));
    log.info("已发布 loginSucc 事件UserLoginEvent01. {}", userDto);
  }

  @Override
  public void loginSucc02(UserDto userDto) {
    Assert.notNull(applicationContext, "ApplicationContext was not init.");
    Assert.notNull(userDto, "传入 UserDto不能为空");
    applicationContext.publishEvent(new UserLoginEvent02(this, userDto));
    log.info("已发布 loginSucc 事件UserLoginEvent02. {}", userDto);
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }
}
